//
//  MYHomeSearchVC.m
//  MuYing
//
//  Created by tbb on 2019/7/23.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYHomeSearchVC.h"
#import "YQNavTitleView.h"
#import "YQSearchBaseBar.h"
#import "MYDynamicDetailsVC.h"
@interface MYHomeSearchVC ()<YQSearchBaseBarDelegate>
@property (nonatomic, strong) YQSearchBaseBar * searchBar;

@property (nonatomic, strong) UIButton * cancelBtn;

@property (strong, nonatomic) NSMutableArray *searchArr;
@end

@implementation MYHomeSearchVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.hidesBackButton = true;

    self.searchArr = NSMutableArray.new;

    if (self.searchBar.searchKey.length != 0) {
        [self setupRefresh];
    }
}

- (void)setupRefresh
{
    self.listTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self searchUserHomeCollectionsApi:YES];
    }];

    self.listTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.searchArr.count < 10) {
            [self.listTableView.mj_footer endRefreshing];
            return ;
        }
        self.page ++;

        [self searchUserHomeCollectionsApi:NO];
    }];
    
    [self.listTableView.mj_header beginRefreshing];
    self.listTableView.mj_header.automaticallyChangeAlpha = YES;

}
#pragma amrk - POST搜索首页帖子
- (void)searchUserHomeCollectionsApi:(BOOL)isReload
{
    if (!self.listTableView.mj_header) {
        [self setupRefresh];
    }
    if (isReload) {
        self.page = 1;
    }
    NSMutableDictionary *dic = NSMutableDictionary.new;

    dic[@"page"] = @(self.page);
    dic[@"limit"] = @(10);
    dic[@"keyword"] = self.searchBar.searchBar.text;
    
    [YQPNetWorkingManager sendPostRequest:kPOST_SEARCH_KEYWORDS parameters:dic success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        if (self.page == 1) {
            [self.searchArr removeAllObjects];
        }
        MYHomeDynamicModel *dynamicModel = [MYHomeDynamicModel mj_objectWithKeyValues:responseObject.data];
        [self.searchArr addObjectsFromArray:dynamicModel.list];
        [self.listTableView reloadData];
        [self.listTableView.mj_header endRefreshing];
        [self.listTableView.mj_footer endRefreshing];

    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        [self.listTableView.mj_header endRefreshing];
        [self.listTableView.mj_footer endRefreshing];
    }];
}

-(void)setupView {
    [super setupView];
    YQNavTitleView *navView = [YQNavTitleView new];
    navView.frame = CGRectMake(0, 0, kScreenWidth, 44);
    navView.intrinsicContentSize = CGSizeMake(kScreenWidth, 44);
    self.navigationItem.titleView = navView;
    
    UIView *bgView = [UIView new];
//    bgView.backgroundColor = UIColor.yellowColor;
    [navView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 44));
    }];
    
    [bgView addSubview:self.searchBar];
    
    [bgView addSubview:self.cancelBtn];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.searchBar.mas_centerY).mas_equalTo(0);
        make.right.mas_equalTo(-12);
        make.height.mas_equalTo(20);
    }];
    
    [self.listTableView registerNib:[UINib nibWithNibName:@"MYHomeSearchHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"MYHomeSearchHeaderView"];
    
    [self.listTableView registerNib:[UINib nibWithNibName:@"MYHomeSearchCell" bundle:nil] forCellReuseIdentifier:@"MYHomeSearchCell"];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.searchArr.count;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MYDynamicModel *model = self.searchArr[indexPath.row];
    MYHomeSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYHomeSearchCell"];
//    cell.contentLb.text = @"子初(Springbuds)母婴本色抽纸婴儿抽取式面巾纸家用纸";
    [cell setModel:model];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 97;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MYDynamicModel *model = self.searchArr[indexPath.row];
    MYDynamicDetailsVC *detailsVC = [[MYDynamicDetailsVC alloc] init];
    detailsVC.invitationId = model.invitationId;
    detailsVC.channelId = model.channelId;
    detailsVC.updateListBlock = ^{
        [self searchUserHomeCollectionsApi:NO];
    };
    kYQPUSH(detailsVC);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    MYHomeSearchHeaderView *heaerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYHomeSearchHeaderView"];
    heaerView.titleLb.hidden = YES;
    if(self.searchArr.count > 0) {
        heaerView.titleLb.hidden = NO;
        NSString *yjRegisterProtocolStr = ([self.searchBar.searchBar.text removeStringSpace].length != 0) ? self.searchBar.searchBar.text : @"xxx";//[NSString stringWithFormat:@"\"%@\"",@"子初"];
        NSString *text = [NSString stringWithFormat:@"%@%@%@",kYQLS(@"搜索到关于"),yjRegisterProtocolStr,kYQLS(@"的帖子")];
        NSMutableAttributedString *addTagStr = [[NSMutableAttributedString alloc] initWithString:text];
        NSRange range = [text rangeOfString:yjRegisterProtocolStr];
        [addTagStr setColor:kAPPThemeColor range:range];
        heaerView.titleLb.attributedText = addTagStr.copy;
    }
    return heaerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 29;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end

#pragma makr -- lazy
-(YQSearchBaseBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [[YQSearchBaseBar alloc] initWithFrame:CGRectMake(12, 0, SCREEN_WIDTH - 68, 28)];
        _searchBar.top = 11;
        _searchBar.delegate = self;
        _searchBar.placeholderStr = kYQLS(@"搜索您想要的帖子");
    }
    return _searchBar;
}

-(UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:0];
        _cancelBtn.titleLabel.font = kSystemMedium(14);
        [_cancelBtn setTitleColor:HexColor(0xFA969D) forState:(UIControlStateNormal)];
        [_cancelBtn setTitle:kYQLS(@"取消") forState:(UIControlStateNormal)];
        ws(self);
        [_cancelBtn addTapAction:^(id  _Nonnull sender) {
            [weakself.navigationController dismissViewControllerAnimated:true completion:nil];
        }];
    }
    return _cancelBtn;
}
#pragma makr -- lazy end

#pragma makr -- YQSearchBaseBarDelegate
- (void)yq_searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self searchUserHomeCollectionsApi:YES];

}
-(void)yq_textFieldShouldClear:(UITextField *)textField {
    
}
-(void)yq_shouldChangeText:(NSString *)text {
    if (text.length == 0) {
        [MBProgressHUD showMessage:kYQLS(@"请输入关键字")];
        return;
    }
    [self searchUserHomeCollectionsApi:YES];
}
#pragma makr -- YQSearchBaseBarDelegate end


@end
